﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SeoWebSite.BLL;
using SeoWebSite.Model;
using Newtonsoft.Json.Linq;
using System.Configuration;

public partial class QoDesk : System.Web.UI.Page
{
    protected string responseText = "";
    protected OS os = new OS();
    protected string member_name = "";
    protected string member_group = "";
    protected string preferences = "";

    string member_id = "";
    string group_id = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (!os.session_exists())
            {
                responseText = "会话不存在";Response.End();
            }
            member_id = os.get_member_id();
            group_id = os.group_id();
            if (string.IsNullOrEmpty(member_id) || string.IsNullOrEmpty(group_id))
            {
                responseText = "成员/用户组 没找到";
                Response.End();
            }
            QOMembersBLL membersbll = new QOMembersBLL();
            QOGroupsBLL groupsbll = new QOGroupsBLL();
            QOPreferencesBLL preferencesbll = new QOPreferencesBLL();
            member_name = membersbll.get_name(member_id);
            member_group = groupsbll.get_name(group_id);
            preferences = this.get_preferences();
        }
    }

    private string get_preferences()
    {
        string member_preference = os.get_member_preference(member_id, group_id);
        preferences = os.get_member_preference("0", "0");
        JObject jo = JObject.Parse(member_preference);
        //假如包含theme id
        if (jo.SelectToken("appearance.themeId") != null)
        {
            QOThemesBLL themesbll = new QOThemesBLL();
            QOThemes model = themesbll.get_by_id(jo.SelectToken("appearance.themeId").ToString());
            JObject theme = new JObject();
            string theme_dir = ConfigurationManager.AppSettings["THEMES_DIR"];
            theme.Add("id", model.id);
            theme.Add("name", (string)model.data["name"]);
            theme.Add("file", theme_dir + (string)model.data["file"]);
            jo.SelectToken("appearance.themeId").Replace(theme);
        }
        //假如包含wallpaper id
        if (jo.SelectToken("background.wallpaperId") != null)
        {
            QOWallpapersBLL wallpapersbll = new QOWallpapersBLL();
            QOwallpapers model = wallpapersbll.get_by_id(jo.SelectToken("background.wallpaperId").ToString());
            JObject theme = new JObject();
            string wallpaper_dir = ConfigurationManager.AppSettings["WALLPAPERS_DIR"];
            theme.Add("id", model.id);
            theme.Add("name", (string)model.data["name"]);
            theme.Add("file", wallpaper_dir + (string)model.data["file"]);
            jo.SelectToken("background.wallpaperId").Replace(theme);
        }
        preferences = jo.ToString().Replace("themeId", "theme").Replace("wallpaperId", "wallpaper");
        return preferences;
    }

    protected string print_modules() {
        JArray response =new JArray();
        List<QOModules> ms = os.get_modules();
        foreach (QOModules item in ms)
        {
            JObject jo = new JObject();
            jo.Add("id",item.id);
            jo.Add("type",item.type);
            jo.Add("className", (string)item.data.SelectToken("client.class"));
            jo.Add("launcher", item.data.SelectToken("client.launcher.config"));
            jo.Add("launcherPaths", item.data.SelectToken("client.launcher.paths"));
            response.Add(jo);
        }
        return response.ToString();
    }
    protected string print_privileges()
    {
        if (string.IsNullOrEmpty(group_id))
        {
            return "{}";
        }
        QOGroupsBLL groupsbll = new QOGroupsBLL();
        string privilege_id = groupsbll.get_privilege_id(group_id);
        if (string.IsNullOrEmpty(privilege_id))
        {
            return "{}";
        }
        QOPrivilegesBLL privilegesbll = new QOPrivilegesBLL();
        return privilegesbll.get_data(int.Parse(privilege_id));
    }
    protected string print_appearance()
    {
        JObject jo = JObject.Parse(preferences);
        return jo["appearance"] != null ? jo["appearance"].ToString() : "{}";
    }
    protected string print_background()
    {
        JObject jo = JObject.Parse(preferences);
        return jo["background"] != null ? jo["background"].ToString() : "{}";
    }
    protected string print_launchers()
    {
        JObject jo = JObject.Parse(preferences);
        return jo["launchers"] != null ? jo["launchers"].ToString() : "{}";
    }

}